package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.forms.ApplicationMappingForm;
import admin.models.ApplicationMappingModel;
import admin.models.ApplicationModel;

import entities.Application;
import entities.ApplicationMapping;

public class ApplicationMappingAddServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		ApplicationMapping appMapping = new ApplicationMapping();
		appMapping.setApplicationId(id);
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("appMapping", appMapping);
		try {
			request.getRequestDispatcher("/admin/pages/application_mapping_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(id);
		
		if (app == null) {
			response.sendRedirect("/admin/appmapping/list");
			return ;
		}
		
		ApplicationMappingModel appMappingModel = ApplicationMappingModel.getInstance();
		ApplicationMapping appMapping = new ApplicationMapping();
		appMapping.setApplicationId(id);
		
		TreeMap form = (TreeMap) ApplicationMappingForm.checkValid(request, appMapping);
		appMapping = (ApplicationMapping) form.get("appMapping");
		TreeMap check = (TreeMap) form.get("checkValid");
		
		if (check.size() == 0) {
			appMappingModel.insert(appMapping);
			request.setAttribute("success", "Add success domain " + appMapping.getDomain());
			appMapping = new ApplicationMapping();
			appMapping.setApplicationId(id);
		}
		
		request.setAttribute("form", "add");
		request.setAttribute("check", check);
		request.setAttribute("appMapping", appMapping);
		try {
			request.getRequestDispatcher("/admin/pages/application_mapping_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
